;
var request = require("request");
var db = require("../../javascript/mysql.js");
//实例化对象
var mysql = new db.mysqlClient("accident");
//生成产品数据
function genProductInfo() {
    var days = ["6m", "1y"];

    for (i = 0; i < days.length; i++) {
        data = {
            "days": days[i],
            "people_count": "1",
            "bus_death": "300000",
            "bicycle_death": "200000",
            "private_death": "0",
            "train_death": "500000",
            "ship_death": "500000",
            "airplane_death": "2000000",
            "accident_ambulance": ""

        };
        httpRequest( data );


    }
    for (i = 0; i < days.length; i++) {
        data = {
            "days": days[i],
            "people_count": "1",
            "bus_death": "200000",
            "bicycle_death": "0",
            "private_death": "200000",
            "train_death": "1000000",
            "ship_death": "1000000",
            "airplane_death": "3000000",
            "accident_ambulance": "1000"

        };
        httpRequest( data );

    }
    for (i = 0; i < days.length; i++) {
        data = {
            "days": days[i],
            "people_count": "1",
            "bus_death": "200000",
            "bicycle_death": "0",
            "private_death": "200000",
            "train_death": "1000000",
            "ship_death": "1000000",
            "airplane_death": "3000000",
            "accident_ambulance": "1500"

        };
        httpRequest( data );

    }
    for (i = 0; i < days.length; i++) {
        data = {
            "days": days[i],
            "people_count": "1",
            "bus_death": "300000",
            "bicycle_death": "300000",
            "private_death": "300000",
            "train_death": "1000000",
            "ship_death": "1000000",
            "airplane_death": "5000000",
            "accident_ambulance": "2000"

        }

    }
    httpRequest( data );


}
//urlencode
function urlencode(data) {
    try {
        var tempArr = [];
        for (var i in data) {
            var key = encodeURIComponent(i);
            var value = encodeURIComponent(data[i]);
            tempArr.push(key + '=' + value);
        }
        var urlParamsStr = tempArr.join('&');
        return urlParamsStr;
    } catch (err) {
        return '';
    }
}
//执行爬虫
function httpRequest( dt ) {
    if( dt.days == "6m"){
        endDate = "2020/11/14"
    }else {
        endDate = "2021/05/14"
    }
    if (dt.private_death == "0"){
        items = "1515141416916"
    }else if ( dt.private_death == "100000"){
        items = "1515141416917"

    }else if( dt.private_death == "200000"){
        items = "1515141416918"
    }else {
        items = "1515141416919"
    }
    data = {
        "startdate": "2020/05/15",
        "enddate": endDate,
        "items": items,
        "peoplecount": "1",
        "planextendvalue": "",
        "productcode":"EAA_J"
    };
    payload = urlencode( data );
    var url = "http://www.epicc.com.cn/eproduct/conproposal/calculate.do";
    request({
        url: url,
        method: "POST",
        json: false,
        headers: {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Cookie": "epicc_tid=epicc.1578282117557422; _gscu_321070471=7828211745c42110; s_fid=51BB77C19E20D92E-16021A351FFE693E; vid=ef0bea9d6c25528ee3c6714b85e0baef; epicc_ntid=AAAAAV6CpgMv3h/HwX4WAg==; svid=0C46E643B8EB2B5549E7AA5CA65A65FB; xY6qiPJDsR=MDAwM2IyNTRmNDgwMDAwMDAwMjUwdmhZYiMxNTg5NDE5MjAy; epiccnewEbsWeb=2LwQxGtkJcSsycEAfY2yxnpOa_EYRJXBr5NWpqqxQkWsFXAiuLC9!-616787198; epiccnewEbsWebeproduct=uyMQvoJUoarAWnEx54T12CmKVmhd20lrtipBBq3VZvNDA5uaZlAh!544855977; YXeuzFnfi6=MDAwM2IyNTRmNDgwMDAwMDAwNzYwOgh0EXIxNTg5NDE5MjA0; personalCenter=tXwQwJoW6SFIKRBtuK0iNnk0zHx8uAfwd2HbSS9hTUGmIqTaDpnv!-86117503; _gscbrs_321070471=1; _gscs_321070471=89419207feeki513|pv:1; _gscm=1; udmp_cm_sign_321070471=1; s_getNewRepeat=1589419209227-Repeat; s_vnum=1609818118180%26vn%3D15; s_invisit=true; s_cc=true; R4XlxlrGUk=MDAwM2IyNTRmNDgwMDAwMDAwNWUwWhNsQl4xNTg5NDE5ODQ4; trkHmClickCoords=450%2C451%2C3546%2Chttp%3A%2F%2Fwww.epicc.com.cn%2Fyiwaibaoxian%2Fquanjiaotongbaoxian%2F%23%2Fmain",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
        },
        body:payload
    },function (err,res,body) {
        if (!err && res.statusCode == 200){
            if (body.indexOf("premium") != -1){
                price = JSON.parse( body )[4]["premium"].toFixed(2);
                data = {
                    "name":"交通工具综合意外保险",
                    "company":"中国人民保险",
                    "price":price
                };
                result = Object.assign( data,dt );
                mysql.insertData( data );
            }
        }

    })

}



